package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.R;
import com.demo.domain.Setmeal;
import com.demo.dto.SetmealDto;
import com.demo.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags="套餐详情")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页条件查询所有数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
//    @GetMapping("/page")
//    public R<Page> page(int page, int pageSize, String name){
//        log.info("前端传来的参数page为 {} pageSize为 {} name为 {}",page,pageSize,name);
//        return setmealService.page(page,pageSize,name);
//    }

    /**
     * 分页查询    代码优化
     * 多表条件分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page> page(int page, int pageSize, String name) {
        log.info("前端传来的参数page为 {} pageSize为 {} name为 {}", page, pageSize, name);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        SetmealDto setmealDto = new SetmealDto();

        if (name != null) {
            setmealDto.setName(name);
        }

        setmealService.pageByParams(setmealDtoPage, setmealDto);

        return R.success(setmealDtoPage);
    }


    /**
     * 添加套餐数据
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    @ApiOperation(value = "添加套餐")
    public R<String> setmealSave(@RequestBody SetmealDto setmealDto) {
        log.info("前端传来的参数setmealDto为 {}", setmealDto.toString());
        return setmealService.setmealSave(setmealDto);
    }

    /**
     * 单删和批量删除套餐信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    @ApiOperation(value = "单删和批量删除套餐信息")
    public R<String> deleteSetmeal(Long[] ids) {
        log.info("前端传来的参数ids为 {}", ids.toString());
        return setmealService.deleteSetmeal(ids);
    }

    /**
     * 修改套餐状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    @ApiOperation(value = "修改套餐状态")
    public R<String> setmealStatusByStatus(@PathVariable int status, Long[] ids) {
        log.info("前端传来的参数status为 {} ids为 {}", status, ids.toString());
        return setmealService.setmealStatusByStatus(status, ids);
    }


    /**
     * 通过id回显数据
     * @param id
     * @return
     */
//    @GetMapping("/{id}")
//    public R<SetmealDto> querySetmealById(@PathVariable Long id){
//        log.info("前端传来的id为 {}" ,id);
//        return setmealService.querySetmealById(id);
//    }

    /**
     * 优化代码
     * 一对多查询信息
     * 通过id回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id回显数据")
    public R<SetmealDto> queryById(@PathVariable Long id) {
        log.info("前端传来的id为 {}", id);
        SetmealDto setmealDto = setmealService.queryById(id);
        return R.success(setmealDto);
    }


    /**
     * 修改套餐信息
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    @ApiOperation(value = "修改套餐信息")
    public R<String> editSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info("前端传来的setmealDto为 {}", setmealDto.toString());
        return setmealService.editSetmeal(setmealDto);
    }


    /**
     * App端展示套餐信息
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId +'_'+#setmeal.status")
    @ApiOperation(value = "App端展示套餐信息")
    public R<List<Setmeal>> setmealListApi(Setmeal setmeal) {
        log.info("前端传来的setmeal为 {}", setmeal.toString());
        return setmealService.setmealListApi(setmeal);
    }


}
